import socket

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(('127.0.0.1', 8001))
socket.listen(5)

while True:
    conn, addr = socket.accept()  # 连接，等待阻塞
    conn.sendall('你好啊'.encode('utf8'))  # 连接成功先推送一条消息
    while True:
        data = conn.recv(1024)  # 接收
        if not data:
            break
        print('收到消息：', data.decode('utf8'))
        conn.sendall(data.decode('utf8').encode('utf8'))
    print(addr, '断开连接')
    conn.close()
socket.close()
